import time
from io import BytesIO

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
# driver.get("http://10.1.6.22/user/login")
# time.sleep(3)
# 判断是否有这个标签
# element = driver.find_element(By.CLASS_NAME, "ant-btn")
# locator = (By.CLASS_NAME, "ant-btn")
# util = WebDriverWait(driver, 1).until(EC.visibility_of_all_elements_located(locator))
# print(util)
# driver.find_element("id", "username").send_keys("script")
# driver.find_element("id", "password").send_keys("123456a.")
# driver.find_element("id", "captcha").send_keys("1111")
# element = driver.find_element(By.CLASS_NAME, "ant-btn")
# element.click()
# EC.visibility_of_element_located()
# print(element)
# find_element 找寻标签
# driver.find_element(By.LINK_TEXT, "登 录").click()
# element = driver.find_element(By.XPATH, '//button[@type="submit"]')
# element.click()
# print(element)
# driver.close()

# 图片爬取
driver.get("https://pic.netbian.com/4kdongman/")
imgs = driver.find_elements(By.XPATH, '//ul[@class="clearfix"]/li/a/img')
# for img in imgs:
#     src = img.get_attribute('src')
#     print(src)

size = imgs[0].size
location = imgs[0].location

print("图1大小:%s" % imgs[0].size)
print("图1边界信息:%s" % imgs[0].location)

from PIL import Image

top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size['width']
screen = driver.get_screenshot_as_png()
screen = Image.open(BytesIO(screen))
cap = screen.crop((left, top, right, bottom))
cap.save('11.png')

time.sleep(15)
driver.close()
